<%-- 
    Document   : profile
    Created on : Apr 25, 2012, 8:50:39 PM
    Author     : Dell
--%>

<%@ page language="java" import="java.sql.*" %>
<%@ page import="java.io.*,java.sql.*,java.util.*,java.text.*,java.text.SimpleDateFormat" %>
<%@ page import="java.util.List"%>  
<%@ page import="java.util.Iterator"%>  
<%@ page import="org.apache.commons.fileupload.*"%>  
<%@ page import="org.apache.commons.fileupload.disk.*"%>  
<%@ page import="org.apache.commons.fileupload.servlet.*"%> 
<%@page contentType="text/html" pageEncoding="UTF-8"%>

<!DOCTYPE html>
<%--jsp:include page="header.jsp"/--%>
<%--
    session.setAttribute("page","profile");
    //dummy
    session.setAttribute("user_name","edo");
--%>
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>Profile</title>
        <script type="text/javascript" src="script/home.js"></script>
    <link rel="stylesheet" type="text/css" title="T1" href="stylesheet/progin.css" id="link_theme" />
	<link rel="stylesheet" type="text/css" title="T1" href="stylesheet/posting.css" />
		<meta name="description" content="RERGag, Make You Laughing" />
		<meta name="keywords" content="laugh" />
		<meta name="author" content="Robertus Theodore" />
		<meta http-equiv="content-type" content="text/html;charset=UTF-8" />
    </head>
    <body>
        	
        <div class="main_container">
            <div id="preview"></div>
<%

            String driver = "org.gjt.mm.mysql.Driver";
            Class.forName(driver).newInstance();
            Connection con_profile=null;
            ResultSet rst_profile=null;
            Statement stmtp=null;
            
            try{
                 String url="jdbc:mysql://localhost:3306/progin_171_13509008?user=root"; 
                con_profile = DriverManager.getConnection(url);
                 stmtp=con_profile.createStatement();
                }
            catch(Exception e){
                    out.println(e.getMessage());
            }


    String queryUser = "SELECT * FROM user where user_name = '"+session.getAttribute("user_name")+"'";
    String queryAchievement = "SELECT achievement_name FROM achievement where id_achievement=(SELECT id_achievement FROM user_achievement where user_name ='"+session.getAttribute("user_name")+"')";
    String queryComment = "SELECT id_comment FROM user_comment where user_name ='"+session.getAttribute("user_name")+"'";
    String queryContent = "SELECT id_content FROM content where user_name ='"+session.getAttribute("user_name")+"'";
    
    String user_name = (String) session.getAttribute("user_name");
    String avatar = "";
    String email = ""; 
    String gender = ""; 
    String about_me = ""; 
            
    ResultSet rUser = stmtp.executeQuery(queryUser);
    while(rUser.next())
               {
      avatar  = rUser.getString("avatar");
    }
    rUser.first();
    while(rUser.next())
               {
      email = rUser.getString("email");
    }
    rUser.first();
    while(rUser.next())
               {
      gender = rUser.getString("gender");
    }
    rUser.first();
    while(rUser.next())
               {
      about_me = rUser.getString("about_me");
    }
    rUser.first();
    
    Integer sum = 0;
    while(rUser.next())
           {
        sum++;
    }
    rUser.first();    
    
    out.println( "<h1>"+user_name+"</h1> </br/>");
    out.println( "<img src='"+avatar+"' height=200px; ></img></br/>");
    out.println( email+"</br/>");
    out.println( gender+"</br/>");
    out.println( about_me+"</br/>");
    out.println( "Achievement : </br>");
   
    ResultSet rAchievement = stmtp.executeQuery(queryAchievement);
    if(sum == 0){
        out.println( "Y U NO Get Achievement?!</br>");
    }else{
            //rAchievement.first();
            while(rAchievement.next())
                        {
            out.println(rAchievement.getString("achievement_name"));
            }
           rAchievement.first();
        }
    
    sum = 0;

    ResultSet rComment = stmtp.executeQuery( queryComment);
    while(rComment.next())
               {
        sum++;
    }
    rComment.first();
   
    out.println("Comments posted : "+sum+"</br>");
    sum = 0;
       
    ResultSet rContent = stmtp.executeQuery(queryContent);
    
    while(rContent.next())
               {
        sum++;
    }
    rContent.first();
    
    out.println("Contents posted : "+sum+"</br>");
       
    String query1 = "select distinct content.content_description, content.id_content, user_name, content.title, content.content_type, content.content_filled from ";
    String query2 = "(select id_content, TIMESTAMPDIFF(SECOND, posting_time, now()) as time from content order by time) time_order, content ";
    String query3 = " where user_name = '"+user_name+"' order by time_order.time ";
    
    ResultSet rTemp = null;
    try{
     rTemp = stmtp.executeQuery(query1+query2+query3);
          }catch (SQLException e){
              out.println("Caught SQL Exception: " + e);
          }//rTemp.first();
    sum = 0;
    while(rTemp.next())
           {
        sum++;
    }    
    
    

    
    if(sum == 0){
        out.println( "No tag exists in database");
    }else{
        out.println("generating  "+sum+" contents</br>");
        //Statement stmtpx = null;    
        rTemp = stmtp.executeQuery(query1+query2+query3);
        
        
        while(rTemp.next()){
            
             if(rTemp.getString("content_type").equals("link"))
                {
                    out.println("<h1>"+rTemp.getString("title")+"</h1>");
                    out.println("<h2>"+rTemp.getString("content_filled")+"</h2>");
                    out.println("<h3>"+rTemp.getString("content_description")+"</h3>");
                    out.println("<h3>Posted by : <a href='detail.jsp'/>"+rTemp.getString("user_name")+"</a></h3>");
                }
                else if(rTemp.getString("content_type").equals("image"))
                {
                    out.println("<h1>"+rTemp.getString("title")+"</h1>");
                    out.println("<img src='"+rTemp.getString("content_filled")+"' height=500px width=this.width; ></img>");
                    out.println("<h3> Posted by : <a href='detail.jsp'/>"+rTemp.getString("user_name")+"</a></h3>");
                    
                }
                else if(rTemp.getString("content_type").equals("video"))
                {
                    out.println("<h1>"+rTemp.getString("title")+"</h1>");
                    out.println(rTemp.getString("content_filled"));
                    out.println("<h3>Posted by : <a href='detail.jsp'/>"+rTemp.getString("user_name")+"</a></h3>");
                    
                }              
            out.println("<br><br>");
                out.println("<div id=infolikes>");
                out.println("<script type='text/javascript' src='script/home.js'></script>");
                out.println("<input id='like_"+rTemp.getInt("id_content")+"' class='like_button' type='image' src='images/like_button.jpg' onclick='like(\""+user_name+"\","+rTemp.getInt("id_content")+")' style='float:left;'/>");
                out.println("<input class='total_like_class' type='text' disabled='disabled' id='total_like_"+rTemp.getInt("id_content")+"'  />");
                out.println("<input id='dislike_'"+rTemp.getInt("id_content")+" class='like_button' type='image' src='images/dislike_button.jpg' onclick='dislike(\""+user_name+"\","+rTemp.getInt("id_content")+")'/>");
                out.println("<input class='total_dislike_class' type='text' disabled='disabled' id='total_dislike_"+rTemp.getInt("id_content")+"'/>");
                out.println(" </div>");
                out.println("<script type='text/javascript'>"); //hitung nilai like dan dislike dulu!
                out.println("likeUpdater("+rTemp.getInt("id_content")+"); dislikeUpdater("+rTemp.getInt("id_content")+");");
                out.println(" </script>");
                Integer nowid = rTemp.getInt("id_content");

                 // Get current datetime
            java.util.Date now_profile = new java.util.Date();
            String DATE_FORMAT = "yyyy-MM-dd hh:mm:ss";
            SimpleDateFormat sdf_profile = new SimpleDateFormat(DATE_FORMAT);
            String strDateNew_profile = sdf_profile.format(now_profile) ;
            
                 // show time Posting
                ResultSet rx = null;
                Statement omega = null;
        }                      
        
    /*           
     String qu1 ="select id_comment,user_name, comment_text, Concat ( Floor(Hour(TIMEDIFF (comment_time, '"+strDateNew_profile+"'))/24), ' days ',";
     String qu2="MOD(HOUR(TIMEDIFF(comment_time, '"+strDateNew_profile+"')), 24), ' hours ',";
     String qu3 ="MINUTE (TIMEDIFF(comment_time, '"+strDateNew_profile+"')), ' minutes')as time from";
     String qu4 = "comment natural join user_comment where comment.id_content ="+nowid+" order by time";
     String querySelectComment= qu1+qu2+qu3+qu4;
                
                
     rx = omega.executeQuery(querySelectComment);
            
              
                String timeDiff =" ";
                while (rx.next()){
                    timeDiff = rx.getString("time_difference");
                }
                out.println("Posting Time:  "+timeDiff+" ago<br/>");
        }*/
       }
    
%>
        <script type="text/javascript" src="script/posting.js"></script>
        <script type="text/javascript" src="script/progin.js"></script>
        </div>
    </body>
</html>
